import { Breadcrumb } from 'antd';
import React, { FC } from 'react';
import { useNavigate, useLocation, useParams } from 'react-router-dom';
import { PageContainer } from '@/component/page-container';
import styles from './index.less';
import Basic from '../component/basic';
import { NewTeachersSrv } from '@/service/teacher';
import { getPathRoot } from '@/utils';

const Groups: FC = () => {
  const navigate = useNavigate();
  const location = useLocation();
  const { id } = useParams();
  console.log('🚀 ~ location:', location);

  const baseApi = {
    basicTop: {
      save: NewTeachersSrv.update,
    },
    basicBody: {
      submit: NewTeachersSrv.infoEditSubmit,
      save: NewTeachersSrv.infoEditSave,
    },
  };

  const [form, setForm] = React.useState({ basicTop: {}, basicBody: {} });

  const init = async () => {
    const resD = await NewTeachersSrv.detail(id);
    if (resD.success) {
      setForm(prevForm => ({
        ...prevForm,
        basicTop: { ...resD.data },
      }));
    }
    // 新增状态不需要请求
    if (location.state?.editType === 'add') return;
    // 无默认状态不需要请求
    // if (location.state?.teacher_base_id === 0) return;
    const resI = await NewTeachersSrv.info(id);
    if (resI.success) {
      if (resI.data.current_post_level) {
        resI.data.current_post_level = resI.data.current_post_level.split(',');
      }
      if (resI.data.health_condition) {
        resI.data.health_condition = resI.data.health_condition.split(',');
      }
      if (resI.data.highest_education) {
        resI.data.highest_education = resI.data.highest_education.split(',');
      }
      if (resI.data.current_post_type) {
        resI.data.current_post_type = resI.data.current_post_type.split(',');
      }
      if (resI.data.source_of_staff) {
        resI.data.source_of_staff = resI.data.source_of_staff.split(',');
      }
      console.log('🚀 ~ init ~ resI.data:', resI.data);
      // changeApi();

      setForm(prevForm => ({
        ...prevForm,
        basicBody: { ...resI.data },
      }));
    }

    console.log('form', form);
  };

  React.useEffect(() => {
    if (!location.state) {
      navigate(`${getPathRoot()}/onboarding`);
      return;
    }
    init();
  }, []);
  return (
    <PageContainer
      header={
        <Breadcrumb
          items={[
            {
              title: '新教职工管理',
              onClick: e => {
                navigate(`${getPathRoot()}/onboarding`);
              },
            },
            { title: '新教职工编辑' },
          ]}
        />
      }
    >
      <div className={styles.base_info}>
        <Basic buttonList={[]} form={form} api={baseApi} />
      </div>
    </PageContainer>
  );
};

export default Groups;
